package com.gang.music.system.entity;

import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.smallchill.core.toolbox.kit.CollectionKit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 
 * </p>
 *
 * @author gang
 * @since 2017-10-26
 */
@TableName("pt_user")
public class User extends Model<User> {

    private static final long serialVersionUID = 1L;

	@TableId(value="id", type= IdType.AUTO)
	private Long id;
	@TableField("login_name")
	private String loginName;
	private String name;
	@TableField(exist = false)
	private String plainPassword;
	private String password;
	private String salt;
	@TableField("register_date")
	private Date registerDate;
	@TableField("lock")
	private boolean lock = false;
	@TableField("pass_error_count")
	private Integer passErrorCount;
	@TableField("unlock_time")
	private Date unlockTime;
	@TableField(exist = false)
	private List<Song> songList = new ArrayList<>();

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getPlainPassword() {
		return plainPassword;
	}

	public void setPlainPassword(String plainPassword) {
		this.plainPassword = plainPassword;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	// 设定JSON序列化时的日期格式
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Date getRegisterDate() {
		return registerDate;
	}

	public void setRegisterDate(Date registerDate) {
		this.registerDate = registerDate;
	}

	public boolean getLock() {
		return lock;
	}

	public void setLock(boolean lock) {
		this.lock = lock;
	}

	public Integer getPassErrorCount() {
		return passErrorCount;
	}

	public void setPassErrorCount(Integer passErrorCount) {
		this.passErrorCount = passErrorCount;
	}

	public Date getUnlockTime() {
		return unlockTime;
	}

	public void setUnlockTime(Date unlockTime) {
		this.unlockTime = unlockTime;
	}
	
	public List<Song> getSongList() {
		return songList;
	}

	public void setSongList(List<Song> songList) {
		this.songList = songList;
	}

	public Long getSongNum() {
		
		if(CollectionKit.isEmpty(songList))
			return 0l;
		return (long) songList.size();
	}

	@Override
	protected Serializable pkVal() {
		return this.id;
	}
	
	public void recovery() {
		
		this.lock = false;
		this.passErrorCount = 0;
	}

	@Override
	public String toString() {
		return "PtUser{" +
			"id=" + id +
			", loginName=" + loginName +
			", name=" + name +
			", password=" + password +
			", salt=" + salt +
			", registerDate=" + registerDate +
			", lock=" + lock +
			", passErrorCount=" + passErrorCount +
			", unlockTime=" + unlockTime +
			"}";
	}
}
